package com.pureut.storage.service.impl;


import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.common.core.web.domain.AjaxResult;
import com.pureut.common.datascope.annotation.DataScope;
import com.pureut.common.security.utils.DictUtils;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.storage.domain.*;
import com.pureut.storage.domain.system.domain.SysDocument;
import com.pureut.storage.dto.*;
import com.pureut.storage.export.PickingRestExport;
import com.pureut.storage.mapper.*;
import com.pureut.storage.service.*;
import com.pureut.storage.utils.DocumentTypeEnum;
import com.pureut.storage.utils.GenerationMethodEnum;
import com.pureut.storage.vo.*;
import com.pureut.system.api.FeignService;
import com.pureut.system.api.QualityFeginService;
import com.pureut.system.api.RemoteUserService;
import com.pureut.system.api.SystemDocumentFeignService;
import com.pureut.system.api.domain.SysDictData;
import com.pureut.system.api.domain.SysUser;
import com.pureut.system.api.vo.MaterialNumberAndBatchNoVo;
import com.pureut.system.api.vo.QualityInspectionDocumentVo;
import com.pureut.system.api.vo.QualitySchemeConfigurationVo;
import com.pureut.system.api.vo.UpdateQualityStatusVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: CM
 * @date: 2022/12/28  17:47
 * @Desc: 其他出库
 */
@Service
public class PickingRestServiceImpl extends ServiceImpl<PickingRestMapper, PickingRest> implements PickingRestService {

    @Resource
    PickingRestMapper pickingRestMapper;

    @Resource
    ProductionPickingService productionPickingService;

    @Resource
    FeignService feignService;

    @Resource
    WarehouseMapper warehouseMapper;

    /**
     * 库存明细
     */
    @Resource
    WmsStockInfoMapper wmsStockInfoMapper;

    /**
     * 库存明细
     */
    @Resource
    WmsStockInfoService wmsStockInfoService;

    /**
     * 其他出库明细
     */
    @Resource
    PickingRestDetailMapper pickingRestDetailMapper;

    /**
     * 其他出库明细
     */
    @Resource
    PickingRestDetailService pickingRestDetailService;

    /**
     * 公共物料表
     */
    @Resource
    PublicGoodsMapper publicGoodsMapper;

    /**
     * 远程调用 feign 用户服务
     */
    @Resource
    RemoteUserService remoteUserService;

    /**
     * 仓库
     */
    @Resource
    WarehouseServiceImpl warehouseService;

    /**
     * 远程调用 feign 单据配置
     */
    @Resource
    SystemDocumentFeignService systemDocumentFeignService;

    @Resource
    ProductionPickingMapper productionPickingMapper;

    @Resource
    private QualityFeginService qualityFeginService;

    @Resource
    private PickingSchemeService pickingSchemeService;

    @Resource
    private OutboundLogService outboundLogService;

    @Resource
    private OutboundLogMapper outboundLogMapper;

    /**
     * 新增
     *
     * @param pickingRestVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addRest(PickingRestVo pickingRestVo) throws Exception {
        List<PickingRestRelationVo> list = pickingRestVo.getList();

        List pickingRestlist = new ArrayList<PickingRest>();



        if (list.size() > 0) {

            for (PickingRestRelationVo pickingRestRelationVo : list) {

                String sn = feignService.getAuthorityCoding("warehouse:outbound:otherout:list");
                if (sn == null) {
                    throw new GlobalException("该单据未进行配置,无法生成单据编码");
                }

                PickingRest pickingRest = new PickingRest();
                BeanUtils.copyProperties(pickingRestVo, pickingRest);
                BeanUtils.copyProperties(pickingRestRelationVo, pickingRest);
                pickingRest.setCreateUser(SecurityUtils.getUsername())
                        .setCreateTime(new Date())
                        .setDocumentType(DocumentTypeEnum.PICKING_REST.getCode())
                        .setWodDocNum(sn)
                        .setDeptId(SecurityUtils.getDeptId())
                        .setWodStatus(1)
                        .setMaterialUnit(pickingRestRelationVo.getMaterialUnit())
                        .setConnectionId(pickingRestRelationVo.getConnectionId())
                        .setGenerationMode(GenerationMethodEnum.MANUALLY_GENERATED.getCode());
                pickingRestlist.add(pickingRest);
            }
            return saveBatch(pickingRestlist);
        } else {
            throw new GlobalException("请至少添加一条物料明细");
        }
    }

    /**
     * 其他出库修改
     *
     * @param pickingRestVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateRest(PickingRestVo pickingRestVo) {

        //查询当前id对应的单据
        PickingRest pickingRest = pickingRestMapper.selectById(pickingRestVo.getId());

        int wodStatus = pickingRest.getWodStatus();
        if (wodStatus != 1) {
            throw new GlobalException("只能对“待出库”状态的单据进行修改");
        }

        List<PickingRestRelationVo> list = pickingRestVo.getList();
        PickingRestRelationVo pickingRestRelationVo = list.get(0);

        //设置修改的值
        pickingRest.setTransceiverType(pickingRestVo.getTransceiverType());
        pickingRest.setDateDelivery(pickingRestVo.getDateDelivery());
        pickingRest.setRemark(pickingRestVo.getRemark());

        pickingRest.setPlannedWeight(pickingRestRelationVo.getPlannedWeight());
        pickingRest.setOutOfStock(pickingRestRelationVo.getOutOfStock());
        pickingRest.setConnectionId(pickingRestRelationVo.getConnectionId());
        return updateById(pickingRest);

    }

    /**
     * 关结其他出库单
     *
     * @param pickingRestVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean closeRest(PickingRestVo pickingRestVo) {
        //查询当前单据的状态，只能对待提交，未通过，待审核，审核中，待出库，待交接，状态的单据进行关结
        PickingRest pickingRest = pickingRestMapper.selectById(pickingRestVo.getId());
        //取出当前的单据状态
        int wodStatus = pickingRest.getWodStatus();

        if (wodStatus==3||wodStatus==4){
            throw new GlobalException("不可对已关结和已完成状态的单据进行重复关结");
        }


        try {
            //如果单据状态是待检验，入库的物料显示在库，关联品质检验单状态更新为撤销
            if(wodStatus==5||wodStatus==6||wodStatus==2){
                //根据单号获取生产领料单明细
                List<PickingRestDetail> pickingRestDetailList = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));

                ArrayList<WmsStockInfo> wmsStockInfos = new ArrayList<>();

                for (PickingRestDetail pickingRestDetail : pickingRestDetailList) {
                    WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, pickingRestDetail.getMaterialNumber()));
                    wmsStockInfos.add(wmsStockInfo);
                }

                if (wmsStockInfos.size()!=0){
                    boolean b = wmsStockInfoService.removeBatchByIds(wmsStockInfos);
                }



                if (wodStatus==5){
                    //如果是待检验删除品质检验单
                    UpdateQualityStatusVo updateQualityStatusVo = new UpdateQualityStatusVo();
                    updateQualityStatusVo.setStatus(8);
                    updateQualityStatusVo.setAssociatedNo(pickingRest.getWodDocNum());
                    AjaxResult ajaxResult = qualityFeginService.updateQualityInspectionDocument(updateQualityStatusVo);


                    if (!"200".equals(ajaxResult.get("code").toString())){
                        throw new GlobalException("服务调用失败");
                    }

                }
            }
        }catch (Exception e){
            throw new GlobalException("关结失败");
        }


        //取出关结人
        String closeMan = pickingRest.getCloseMan();
        if (closeMan!=null){
            closeMan=closeMan+","+SecurityUtils.getUsername();
        }else{
            closeMan=SecurityUtils.getUsername();
        }


        //修改单据状态未关结，修改关结原因，修改关结人，修改关结时间
        PickingRest pickingRest1 = new PickingRest();
        pickingRest1.setId(pickingRestVo.getId())
                .setWodStatus(4).setCloseTime(new Date())
                .setCloseMan(closeMan)
                .setCloseCause(pickingRestVo.getCloseCause());
        return updateById(pickingRest1);
    }

    /**
     * 删除其他出库单
     *
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteRest(String ids) {
        //将ids根据逗号拆分成数组
        List<String> idStr = Arrays.asList(ids.split(","));
        if (idStr.size() == 0) {
            throw new GlobalException("请至少勾选一条单据");
        }
        //根据ids批量查询出list列表
        List<PickingRest> pickingRests = pickingRestMapper.selectBatchIds(idStr);

        //遍历
        for (PickingRest pickingRest : pickingRests) {
            if (pickingRest.getWodStatus() != 1) {
                throw new GlobalException("只能删除“待出库”状态的单据");
            } else if (pickingRest.getGenerationMode() != 2) {
                throw new GlobalException("系统自动生成的单据不可删除");
            }
        }
        return removeBatchByIds(idStr);
    }

    /**
     * 查看出库单
     *
     * @param id
     * @return
     */
    @Override
    public PickingRestDto view(Long id) {
        //查询单条对象

        PickingRestDto pickingRestDto = pickingRestMapper.getPickingById(id);
        //根据字典查询收发类型
        List<SysDictData> statusArray = DictUtils.getDictCache("picking_rest_type");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //根据字典查出单位
        List<SysDictData> unitList = DictUtils.getDictCache("sys_material_unit");
        Map<String, String> unitMap = unitList.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));


        //添加单据类型
        pickingRestDto.setDocumentTypeDict(DocumentTypeEnum.getValue(Integer.parseInt(pickingRestDto.getDocumentType())));
        pickingRestDto.setDocumentType(pickingRestDto.getDocumentType());

        //添加收发类型
        pickingRestDto.setTransceiverType(pickingRestDto.getTransceiverType() + "");
        pickingRestDto.setTransceiverTypeDict(statusMap.get(pickingRestDto.getTransceiverType() + ""));

        List<PickingRestViewDto> list = new ArrayList<>();
        PickingRestViewDto pickingRestViewDto = new PickingRestViewDto();

        //添加单位key
        pickingRestViewDto.setMaterialUnit(pickingRestDto.getMaterialUnit() + "");
        //添加单位值
        pickingRestViewDto.setMaterialUnitDict(unitMap.get(pickingRestDto.getMaterialUnit() + ""));


        //查看仓库名称
        Warehouse warehouse = warehouseMapper.selectById(pickingRestDto.getOutOfStock());
        if (warehouse != null) {
            //添加仓库名称
            pickingRestViewDto.setOutOfStockDict(warehouse.getWarehouseName());
            pickingRestViewDto.setOutOfStock(pickingRestDto.getOutOfStock() + "");
        }

        //添加物料名称,物料规格，物料料号
        pickingRestViewDto.setMaterialCode(pickingRestDto.getMaterialCode());
        pickingRestViewDto.setMaterialName(pickingRestDto.getItemName());
        pickingRestViewDto.setMaterialSpec(pickingRestDto.getItemSpec());
        //添加计划重量
        pickingRestViewDto.setPlannedWeight(pickingRestDto.getPlannedWeight());
        //添加关联单号
        pickingRestViewDto.setConnectionId(pickingRestDto.getConnectionId());
        list.add(pickingRestViewDto);
        pickingRestDto.setList(list);
        return pickingRestDto;
    }

    /**
     * 获取其他出库列表
     *
     * @param pickingRestVo
     * @return
     */
    @Override
    @DataScope(deptAlias = "t1")
    public List<PickingRestDto> getList(PickingRestVo pickingRestVo) {
        //查询委外出库列表
        List<PickingRestDto> pickingRestsList = pickingRestMapper.getPickingRestList(pickingRestVo);

        //获取单据状态字典数据
        List<SysDictData> statusArray = DictUtils.getDictCache("picking_rest_status");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //根据字典查出收发类型
        List<SysDictData> receivType = DictUtils.getDictCache("picking_rest_type");
        Map<String, String> typeMap = receivType.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //根据字典查出单位
        List<SysDictData> unitList = DictUtils.getDictCache("sys_material_unit");
        Map<String, String> unitMap = unitList.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));


        //新建一个dto集合
        for (PickingRestDto pickingRestDto : pickingRestsList) {

            //查看仓库名称
            Warehouse warehouse = warehouseMapper.selectById(pickingRestDto.getOutOfStock());
            if (warehouse != null) {
                //添加仓库名称
                pickingRestDto.setOutOfStock(warehouse.getWarehouseName());
                pickingRestDto.setOutOfStockDict(pickingRestDto.getOutOfStock() + "");
            }

            //添加单据类型
            pickingRestDto.setDocumentType(DocumentTypeEnum.getValue(Integer.parseInt(pickingRestDto.getDocumentType())));

            //添加收发类型
            pickingRestDto.setTransceiverTypeDict(pickingRestDto.getTransceiverType() + "");
            pickingRestDto.setTransceiverType(typeMap.get(pickingRestDto.getTransceiverType() + ""));

            //添加单据状态
            pickingRestDto.setWodStatusDict(pickingRestDto.getWodStatus() + "");
            pickingRestDto.setWodStatus(statusMap.get(pickingRestDto.getWodStatus() + ""));

            //添加单位key
            pickingRestDto.setMaterialUnitDict(pickingRestDto.getMaterialUnit() + "");

            //添加单位值
            pickingRestDto.setMaterialUnit(unitMap.get(pickingRestDto.getMaterialUnit() + ""));

            //添加生产方式
            pickingRestDto.setGenerationMode(GenerationMethodEnum.getValue(Integer.parseInt(pickingRestDto.getGenerationMode())));

        }
        return pickingRestsList;
    }

    @Override
    @DataScope(deptAlias = "t1")
    public List<GetPdaDto> getListPda(PickingRestVo pickingRestVo) {
        //查询委外出库列表
        List<GetPdaDto> list = pickingRestMapper.getPickingRestListPda(pickingRestVo);

        //获取单据状态字典数据
        List<SysDictData> statusArray = DictUtils.getDictCache("picking_rest_status");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        for (GetPdaDto getPdaDto : list) {
            getPdaDto.setWodStatusDict(statusMap.get(getPdaDto.getWodStatus()));
        }


        return list;
    }


    /**
     * 导出
     *
     * @param pickingRestVo
     * @return
     */
    @Override
    public List<PickingRestExport> getInfoExport(PickingRestVo pickingRestVo) {

        if (pickingRestVo.getIdStr()!=null) {

            List<String> idList = Arrays.asList(pickingRestVo.getIdStr().split(","));

            pickingRestVo.setIds(idList);
        }

        List<PickingRestExport> list = pickingRestMapper.getInfoExport(pickingRestVo);

        //根据字典查出收发类型
        List<SysDictData> receivType = DictUtils.getDictCache("picking_rest_type");
        Map<String, String> typeMap = receivType.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        for (PickingRestExport pickingRestExport : list) {
            pickingRestExport.setTransceiverTypeDict(typeMap.get(pickingRestExport.getTransceiverTypeDict()));
        }

        return list;
    }

    /**
     * 其他出库(PDA)
     *
     * @param pickingRestDbVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PickingRestDbDto pickingRestIssue(PickingRestDbVo pickingRestDbVo) throws Exception {
        // 出库方式
        String type = pickingRestDbVo.getType();

        // 输入值
        String inputValue = pickingRestDbVo.getInputValue();

        PickingRestDbDto pickingRestDbDto = new PickingRestDbDto();

        //判断是否超收或者超发
        //获取菜单id
        Long menuId = feignService.getIdByMenuName("其他出库");
        AjaxResult infoDocumentConfigById = systemDocumentFeignService.getInfoDocumentConfigById(menuId);
        SysDocument sysDocument = JSON.parseObject(JSON.toJSONString(infoDocumentConfigById.get("data")), SysDocument.class);

        //根据字典查出单位
        List<SysDictData> unitList = DictUtils.getDictCache("sys_material_unit");
        Map<String, String> unitMap = unitList.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        //如果配置了送检,则校验是否配了检验方案
        if (sysDocument.getDocInspectionMark()==1){

            List<SysDocumentCheckDto> documentCheckDtoList = sysDocument.getDocumentCheckDtoList();
            if (documentCheckDtoList==null){
                throw new GlobalException("请先在单据配置中配置送检方案");
            }


            PickingRest byId = getById(pickingRestDbVo.getId());

            //查询单位
            PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, byId.getMaterialCode()));


            QualitySchemeConfigurationVo qualitySchemeConfigurationVo = new QualitySchemeConfigurationVo();
            qualitySchemeConfigurationVo.setCategory(publicGoods.getMaterialCategory());
            qualitySchemeConfigurationVo.setItemRank(documentCheckDtoList.get(0).getCheckSubmit());
            qualitySchemeConfigurationVo.setMaterialCategory(publicGoods.getMaterialType());
            qualitySchemeConfigurationVo.setMaterialCode(byId.getMaterialCode());
            boolean check = qualityFeginService.check(qualitySchemeConfigurationVo);

            if (!check){
                throw new GlobalException("该物料不存在对应的检验方案");

            }
        }

        // 扫码出库
        if ("1".equals(type)) {

            //判断扫描到的物料编码是否和单据内需要的物料编码一致
            WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, inputValue).eq(WmsStockInfo::getWsiMaterialStatus, 2));
            if (wmsStockInfo == null) {
                pickingRestDbDto.setCode("500");
                pickingRestDbDto.setMessage("扫描失败，请核实后扫描");
                return pickingRestDbDto;
            }

            if ("1".equals(wmsStockInfo.getWsiFreezeInfo())) {
                throw new GlobalException("此物料已冻结请重新输入");
            }

            //扫描的物料编码
            String wsiMaterialCode = wmsStockInfo.getWsiMaterialCode();

            // 根据物料编码在其他出库中查询是否存在
            PickingRest pickingRest = pickingRestMapper.selectById(pickingRestDbVo.getId());
            if (!pickingRest.getMaterialCode().equals(wsiMaterialCode)) {
                pickingRestDbDto.setMessage("500");
                pickingRestDbDto.setMessage("扫描失败，请核实后扫描");
                return pickingRestDbDto;
            }

            // 判断是否重复扫描
            PickingRestDetail pickingRestDetail = pickingRestDetailMapper.selectOne(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getMaterialNumber, pickingRestDbVo.getInputValue()).eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));
            if (pickingRestDetail != null) {
                pickingRestDbDto.setMessage("500");
                pickingRestDbDto.setMessage("扫描失败，请核实后扫描");
                return pickingRestDbDto;
            }

            // 根据物料编码查询物料公共表数据
            String materialCode = pickingRest.getMaterialCode();
            PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, materialCode));

            // 添加其他出库明细
            PickingRestDetail pickingRestDetail1 = new PickingRestDetail();
            // 物料号码
            pickingRestDetail1.setMaterialNumber(wmsStockInfo.getWsiMaterialNumber());
            // 物料编码
            pickingRestDetail1.setMaterialCode(wmsStockInfo.getWsiMaterialCode());
            // 物料状态（出库中）
            pickingRestDetail1.setMaterialStatus(3);
            // 毛重
            pickingRestDetail1.setGrossWeight(wmsStockInfo.getWsiGorssWeight());
            // 净重
            pickingRestDetail1.setNetWeight(wmsStockInfo.getWsiNetWeight());
            // 出库数量
            pickingRestDetail1.setOutgoingNumber(pickingRest.getOutgoingNumber());
            // 配比
            pickingRestDetail1.setRatiio(publicGoods.getMaterialRatio());
            // 克重
            pickingRestDetail1.setGramWeight(wmsStockInfo.getWsiGrawWeight());
            // 厚重
            pickingRestDetail1.setThick(wmsStockInfo.getWsiThick());
            // 幅宽
            pickingRestDetail1.setBreadth(wmsStockInfo.getWsiWidth());
            // 卷长
            pickingRestDetail1.setRollLength(wmsStockInfo.getWsiLength());
            // 网型
            pickingRestDetail1.setMeshType(publicGoods.getMaterialNet());
            // 铺网类型
            pickingRestDetail1.setTypeOfLaying(publicGoods.getMaterialNetType());
            // 仓储信息
            pickingRestDetail1.setStockInfo(wmsStockInfo.getWsiWarehourseInfo());
            // 出库人
            pickingRestDetail1.setDepositor(SecurityUtils.getUsername());
            // 出库单号
            pickingRestDetail1.setWodDocNum(pickingRest.getWodDocNum());
            //批次
            pickingRestDetail1.setMaterialBatch(wmsStockInfo.getWsiMaterialBatch());


            //判断是否有检验
            int docInspectionMark = sysDocument.getDocInspectionMark();
            //如果有检验
            if (docInspectionMark == 1) {
                //修改单据明细为待检验
                pickingRestDetail1.setMaterialStatus(1);
            } else {
                //判断是否有交接
                int docHandoverMark = sysDocument.getDocHandoverMark();
                //有交接
                if (docHandoverMark == 1) {
                    //修改单据明细为待交接
                    pickingRestDetail1.setMaterialStatus(2);

                }
            }

            // 添加明细
            int insert = pickingRestDetailMapper.insert(pickingRestDetail1);

            if (insert <= 0) {
                throw new GlobalException("出库失败");
            }

            //修改委外发货单的单据状态为出库中
            pickingRest.setWodStatus(2);
            //修改出库重量和出库数量
            Double outgoingWeight = pickingRest.getOutgoingWeight();
            if (outgoingWeight == null) {
                outgoingWeight = 0.0;
            }
            BigDecimal bigDecimal = new BigDecimal(String.valueOf(outgoingWeight));

            BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(wmsStockInfo.getWsiNetWeight()));

            BigDecimal add = bigDecimal.add(bigDecimal1);
            pickingRest.setOutgoingWeight(add.doubleValue());

            Integer num = pickingRest.getOutgoingNumber();
            if (num == null) {
                num = 0;
            }
            pickingRest.setOutgoingNumber(num + 1);

            //判断单位是否是kg
            if ("2".equals(publicGoods.getMaterialUnit())) {
                //实际量
                pickingRest.setActualNumber(pickingRest.getOutgoingWeight());
            }else if("3".equals(publicGoods.getMaterialUnit())){
                //实际量
                BigDecimal dun = new BigDecimal("1000");
                BigDecimal outNum = new BigDecimal(String.valueOf(pickingRest.getOutgoingWeight()));
                BigDecimal divide = outNum.divide(dun);
                pickingRest.setActualNumber(divide.doubleValue());
            }else {
                //实际量
                pickingRest.setActualNumber(Double.parseDouble(pickingRest.getOutgoingNumber() + ""));
            }
            updateById(pickingRest);

            // 录入成功后修改仓库状态为备料
            wmsStockInfo.setWsiMaterialStatus(5);
            int i = wmsStockInfoMapper.updateById(wmsStockInfo);

            //返回前端参数
            //计划/实际
            pickingRestDbDto.setQuantityIdentification(pickingRest.getPlannedWeight() + "/" + pickingRest.getActualNumber() + unitMap.get(publicGoods.getMaterialUnit()));
            //出库数量
            pickingRestDbDto.setAllotNumOut(pickingRest.getOutgoingNumber() + "");

            pickingRestDbDto.setMessage("物料号码:" + inputValue + ",净重:" + wmsStockInfo.getWsiNetWeight() + ",数量:1" + "录入成功");

            OutboundLog outboundLog = new OutboundLog();
            BeanUtils.copyProperties(wmsStockInfo, outboundLog, new String[]{"id"});
            outboundLogService.save(outboundLog);

            //拣货方案
            SchemeDto point = point(pickingRestDbVo.getId());


            if (point.getCombinationName()!=null){
                pickingRestDbDto.setPickingPrompt(point.getCombinationName());
            }

            //查询当前生产领料单
            PickingRest pickingRest1 = pickingRestMapper.selectById(pickingRestDbVo.getId());

            //判断是否有超收
            int docHairMark = sysDocument.getDocHairMark();
            //没有超收
            if (docHairMark != 1) {

                //如果是kg
                if ("2".equals(publicGoods.getMaterialUnit())||"3".equals(publicGoods.getMaterialUnit())) {
                    //计划出库重量
                    Double allotAmount = pickingRest1.getPlannedWeight();
                    //实际出库重量
                    Double allotWeightOut = pickingRest1.getActualNumber();
                    if (allotWeightOut >= allotAmount) {
                        //判断是否有检验或者交接
                        //判断是否有检验
                        int isDocInspectionMark = sysDocument.getDocInspectionMark();
                        //如果有检验
                        if (isDocInspectionMark == 1) {
                            //生成品质检验单
                            QualityInspectionDocumentVo qualityInspectionDocumentVo = new QualityInspectionDocumentVo();
                            //添加类别，编码，物料类别
                            qualityInspectionDocumentVo.setMaterialCode(pickingRest.getMaterialCode());
                            qualityInspectionDocumentVo.setMaterialCategory(publicGoods.getMaterialType());
                            qualityInspectionDocumentVo.setCategory(publicGoods.getMaterialCategory());
                            qualityInspectionDocumentVo.setAssociatedNo(pickingRest.getWodDocNum());
                            qualityInspectionDocumentVo.setModeType(1);
                            List<MaterialNumberAndBatchNoVo> materialNumberAndBatchNoVos = new ArrayList<>();
                            //查询当前明细表中的所有物料号码
                            List<PickingRestDetail> stockOutInfoDetailList = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));

                            String batchNum = "";

                            for (int j = 0; j < stockOutInfoDetailList.size(); j++) {
                                if (j == 0) {
                                    batchNum = stockOutInfoDetailList.get(j).getMaterialBatch();
                                } else {
                                    if (!batchNum.equals(stockOutInfoDetailList.get(j).getMaterialBatch())) {
                                        batchNum = batchNum + "," + stockOutInfoDetailList.get(j).getMaterialBatch();
                                    }
                                }

                                MaterialNumberAndBatchNoVo materialNumberAndBatchNoVo = new MaterialNumberAndBatchNoVo();
                                materialNumberAndBatchNoVo.setMaterialNumber(stockOutInfoDetailList.get(j).getMaterialNumber());
                                materialNumberAndBatchNoVo.setBatchNo(stockOutInfoDetailList.get(j).getMaterialBatch());
                                materialNumberAndBatchNoVo.setMaterialCategory(publicGoods.getMaterialCategory());
                                materialNumberAndBatchNoVos.add(materialNumberAndBatchNoVo);
                            }
                            qualityInspectionDocumentVo.setBatchNo(batchNum);

                            productionPickingService.insertQuality(sysDocument, qualityInspectionDocumentVo, materialNumberAndBatchNoVos);
                            //修改单据为待检验
                            pickingRest1.setWodStatus(5);
                            pickingRestMapper.updateById(pickingRest1);
                            pickingRestDbDto.setCode("200");
                            return pickingRestDbDto;
                        }
                        //判断是否有交接
                        int docHandoverMark = sysDocument.getDocHandoverMark();
                        //有交接
                        if (docHandoverMark == 1) {
                            //修改单据为待交接
                            pickingRest1.setWodStatus(6);
                            pickingRestMapper.updateById(pickingRest1);

                        } else {
                            //修改单据为yiwanc
                            pickingRest1.setWodStatus(3);
                            //删除库存
                            wmsStockInfoMapper.delete(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, inputValue));
                            pickingRestMapper.updateById(pickingRest1);
                        }
                    }
                } else {
                    //计划出库重量
                    Double allotAmount = Double.parseDouble(pickingRest1.getPlannedWeight() + "");
                    //实际出库数量
                    Integer allotNumOut = pickingRest1.getOutgoingNumber();
                    if (allotNumOut >= allotAmount) {
                        //判断是否检验或者交接
                        //判断是否有检验
                        int isDocInspectionMark = sysDocument.getDocInspectionMark();
                        //如果有检验
                        if (isDocInspectionMark == 1) {
                            //生成品质检验单
                            QualityInspectionDocumentVo qualityInspectionDocumentVo = new QualityInspectionDocumentVo();
                            //添加类别，编码，物料类别
                            qualityInspectionDocumentVo.setMaterialCode(pickingRest.getMaterialCode());
                            qualityInspectionDocumentVo.setMaterialCategory(publicGoods.getMaterialType());
                            qualityInspectionDocumentVo.setCategory(publicGoods.getMaterialCategory());
                            qualityInspectionDocumentVo.setAssociatedNo(pickingRest.getWodDocNum());
                            qualityInspectionDocumentVo.setModeType(1);
                            List<MaterialNumberAndBatchNoVo> materialNumberAndBatchNoVos = new ArrayList<>();
                            //查询当前明细表中的所有物料号码
                            List<PickingRestDetail> stockOutInfoDetailList = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));

                            String batchNum = "";

                            for (int j = 0; j < stockOutInfoDetailList.size(); j++) {
                                if (j == 0) {
                                    batchNum = stockOutInfoDetailList.get(j).getMaterialBatch();
                                } else {
                                    if (!batchNum.equals(stockOutInfoDetailList.get(j).getMaterialBatch())) {
                                        batchNum = batchNum + "," + stockOutInfoDetailList.get(j).getMaterialBatch();
                                    }
                                }

                                MaterialNumberAndBatchNoVo materialNumberAndBatchNoVo = new MaterialNumberAndBatchNoVo();
                                materialNumberAndBatchNoVo.setMaterialNumber(stockOutInfoDetailList.get(j).getMaterialNumber());
                                materialNumberAndBatchNoVo.setBatchNo(stockOutInfoDetailList.get(j).getMaterialBatch());
                                materialNumberAndBatchNoVo.setMaterialCategory(publicGoods.getMaterialCategory());
                                materialNumberAndBatchNoVos.add(materialNumberAndBatchNoVo);
                            }
                            qualityInspectionDocumentVo.setBatchNo(batchNum);

                            productionPickingService.insertQuality(sysDocument, qualityInspectionDocumentVo, materialNumberAndBatchNoVos);
                            //修改单据为待检验
                            pickingRest1.setWodStatus(5);
                            pickingRestMapper.updateById(pickingRest1);
                            pickingRestDbDto.setCode("200");
                            return pickingRestDbDto;
                        }
                        //判断是否有交接
                        int docHandoverMark = sysDocument.getDocHandoverMark();
                        //有交接
                        if (docHandoverMark == 1) {
                            //修改单据明细为待交接
                            pickingRest1.setWodStatus(6);
                            pickingRestMapper.updateById(pickingRest1);

                        } else {
                            //修改单据状态为已完成
                            pickingRest1.setWodStatus(3);
                            //删除库存
                            wmsStockInfoMapper.delete(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, inputValue));
                            pickingRestMapper.updateById(pickingRest1);
                        }
                    }
                }
            }




            // 批次出库
        } else if ("2".equals(type)) {

            // 判断扫描到的批次号是否和单据内的批次号一致
            List<WmsStockInfo> wmsStockInfoList = wmsStockInfoMapper.selectList(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialBatch, pickingRestDbVo.getInputValue()).eq(WmsStockInfo::getWsiMaterialStatus,"2").notIn(WmsStockInfo::getWsiFreezeInfo,1));
            if (wmsStockInfoList.size() == 0) {
                pickingRestDbDto.setMessage("扫描失败，当前批次在库存中为0");
                pickingRestDbDto.setCode("500");
                return pickingRestDbDto;
            }

            //查询当前批次(在库的重量之和)
            Double netWeight = wmsStockInfoMapper.getNetWeight(inputValue);

            //查询委外出库单中的物料编码
            PickingRest pickingRest = pickingRestMapper.selectById(pickingRestDbVo.getId());

            PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, pickingRest.getMaterialCode()));

            List<PickingRestDetail> list = new ArrayList<PickingRestDetail>();

            List<OutboundLog> outboundLogList = new ArrayList<OutboundLog>();

            for (WmsStockInfo wmsStockInfo : wmsStockInfoList) {


                //扫描的物料编码
                String wsiMaterialCode = wmsStockInfo.getWsiMaterialCode();

                if (!pickingRest.getMaterialCode().equals(wsiMaterialCode)) {
                    pickingRestDbDto.setMessage("扫描失败，请核实后扫描");
                    pickingRestDbDto.setCode("500");
                    return pickingRestDbDto;
                }


                // 添加其他出库明细
                PickingRestDetail pickingRestDetail1 = new PickingRestDetail();
                // 物料号码
                pickingRestDetail1.setMaterialNumber(wmsStockInfo.getWsiMaterialNumber());
                // 物料编码
                pickingRestDetail1.setMaterialCode(wmsStockInfo.getWsiMaterialCode());
                // 物料状态（出库中）
                pickingRestDetail1.setMaterialStatus(3);
                // 毛重
                pickingRestDetail1.setGrossWeight(wmsStockInfo.getWsiGorssWeight());
                // 净重
                pickingRestDetail1.setNetWeight(wmsStockInfo.getWsiNetWeight());
                // 出库数量
                pickingRestDetail1.setOutgoingNumber(pickingRest.getOutgoingNumber());
                // 配比
                pickingRestDetail1.setRatiio(publicGoods.getMaterialRatio());
                // 克重
                pickingRestDetail1.setGramWeight(wmsStockInfo.getWsiGrawWeight());
                // 厚重
                pickingRestDetail1.setThick(wmsStockInfo.getWsiThick());
                // 幅宽
                pickingRestDetail1.setBreadth(wmsStockInfo.getWsiWidth());
                // 卷长
                pickingRestDetail1.setRollLength(wmsStockInfo.getWsiLength());
                // 网型
                pickingRestDetail1.setMeshType(publicGoods.getMaterialNet());
                // 铺网类型
                pickingRestDetail1.setTypeOfLaying(publicGoods.getMaterialNetType());
                // 仓储信息
                pickingRestDetail1.setStockInfo(wmsStockInfo.getWsiWarehourseInfo());
                // 出库人
                pickingRestDetail1.setDepositor(SecurityUtils.getUsername());
                // 出库单号
                pickingRestDetail1.setWodDocNum(pickingRest.getWodDocNum());
                //批次
                pickingRestDetail1.setMaterialBatch(wmsStockInfo.getWsiMaterialBatch());


                //判断是否有检验
                int docInspectionMark = sysDocument.getDocInspectionMark();
                //如果有检验
                if (docInspectionMark == 1) {
                    //修改单据明细为待检验
                    pickingRestDetail1.setMaterialStatus(1);
                } else {
                    //判断是否有交接
                    int docHandoverMark = sysDocument.getDocHandoverMark();
                    //有交接
                    if (docHandoverMark == 1) {
                        //修改单据明细为待交接
                        pickingRestDetail1.setMaterialStatus(2);

                    }
                }

                list.add(pickingRestDetail1);


                //修改出库重量和出库数量
                Double outgoingWeight = pickingRest.getOutgoingWeight();
                if (outgoingWeight == null) {
                    outgoingWeight = 0.0;
                }
                BigDecimal bigDecimal = new BigDecimal(String.valueOf(outgoingWeight));

                BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(wmsStockInfo.getWsiNetWeight()));

                BigDecimal add = bigDecimal.add(bigDecimal1);
                pickingRest.setOutgoingWeight(add.doubleValue());

                Integer num = pickingRest.getOutgoingNumber();
                if (num == null) {
                    num = 0;
                }
                pickingRest.setOutgoingNumber(num + 1);


                // 录入成功后修改仓库状态为备料
                wmsStockInfo.setWsiMaterialStatus(5);

                OutboundLog outboundLog = new OutboundLog();
                BeanUtils.copyProperties(wmsStockInfo, outboundLog, new String[]{"id"});
                outboundLogList.add(outboundLog);

            }

            // 添加明细
            pickingRestDetailService.saveBatch(list);
            wmsStockInfoService.updateBatchById(wmsStockInfoList);

            //修改委外发货单的单据状态为出库中
            pickingRest.setWodStatus(2);
            //判断单位是否是kg
            if ("2".equals(publicGoods.getMaterialUnit())) {
                //实际量
                pickingRest.setActualNumber(pickingRest.getOutgoingWeight());
            }else if("3".equals(publicGoods.getMaterialUnit())){
                //实际量
                BigDecimal dun = new BigDecimal("1000");
                BigDecimal outNum = new BigDecimal(String.valueOf(pickingRest.getOutgoingWeight()));
                BigDecimal divide = outNum.divide(dun);
                pickingRest.setActualNumber(divide.doubleValue());
            } else {
                //实际量
                pickingRest.setActualNumber(Double.parseDouble(pickingRest.getOutgoingNumber() + ""));
            }
            updateById(pickingRest);

            PickingRest pickingRest1 = pickingRestMapper.selectById(pickingRestDbVo.getId());
            //返回前端参数
            //计划/实际
            pickingRestDbDto.setQuantityIdentification(pickingRest1.getPlannedWeight() + "/" + pickingRest1.getActualNumber() + unitMap.get(publicGoods.getMaterialUnit()));
            //出库数量
            pickingRestDbDto.setAllotNumOut(pickingRest1.getOutgoingNumber() + "");


            pickingRestDbDto.setMessage("批次号:" + inputValue + ",总净重:" + netWeight + ",数量:" + wmsStockInfoList.size() + "录入成功");

            outboundLogService.saveBatch(outboundLogList);
            pickingRestDbDto.setCode("200");
            //拣货方案
            SchemeDto point = point(pickingRestDbVo.getId());


            if (point.getCombinationName()!=null){
                pickingRestDbDto.setPickingPrompt(point.getCombinationName());
            }

            //判断是否有超收
            int docHairMark = sysDocument.getDocHairMark();
            //没有超收
            if (docHairMark != 1) {

                //如果是kg
                if ("2".equals(publicGoods.getMaterialUnit())||"3".equals(publicGoods.getMaterialUnit())) {
                    //计划出库重量
                    Double allotAmount = pickingRest1.getPlannedWeight();
                    //实际出库重量
                    Double allotWeightOut = pickingRest1.getActualNumber();
                    if (allotWeightOut >= allotAmount) {
                        //判断是否有检验或者交接
                        //判断是否有检验
                        int isDocInspectionMark = sysDocument.getDocInspectionMark();
                        //如果有检验
                        if (isDocInspectionMark == 1) {
                            //生成品质检验单
                            QualityInspectionDocumentVo qualityInspectionDocumentVo = new QualityInspectionDocumentVo();
                            //添加类别，编码，物料类别
                            qualityInspectionDocumentVo.setMaterialCode(pickingRest.getMaterialCode());
                            qualityInspectionDocumentVo.setMaterialCategory(publicGoods.getMaterialType());
                            qualityInspectionDocumentVo.setCategory(publicGoods.getMaterialCategory());
                            qualityInspectionDocumentVo.setAssociatedNo(pickingRest.getWodDocNum());
                            qualityInspectionDocumentVo.setModeType(1);
                            List<MaterialNumberAndBatchNoVo> materialNumberAndBatchNoVos = new ArrayList<>();
                            //查询当前明细表中的所有物料号码
                            List<PickingRestDetail> stockOutInfoDetailList = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));

                            String batchNum = "";

                            for (int j = 0; j < stockOutInfoDetailList.size(); j++) {
                                if (j == 0) {
                                    batchNum = stockOutInfoDetailList.get(j).getMaterialBatch();
                                } else {
                                    if (!batchNum.equals(stockOutInfoDetailList.get(j).getMaterialBatch())) {
                                        batchNum = batchNum + "," + stockOutInfoDetailList.get(j).getMaterialBatch();
                                    }
                                }

                                MaterialNumberAndBatchNoVo materialNumberAndBatchNoVo = new MaterialNumberAndBatchNoVo();
                                materialNumberAndBatchNoVo.setMaterialNumber(stockOutInfoDetailList.get(j).getMaterialNumber());
                                materialNumberAndBatchNoVo.setBatchNo(stockOutInfoDetailList.get(j).getMaterialBatch());
                                materialNumberAndBatchNoVo.setMaterialCategory(publicGoods.getMaterialCategory());
                                materialNumberAndBatchNoVos.add(materialNumberAndBatchNoVo);
                            }
                            qualityInspectionDocumentVo.setBatchNo(batchNum);

                            productionPickingService.insertQuality(sysDocument, qualityInspectionDocumentVo, materialNumberAndBatchNoVos);
                            //修改单据为待检验
                            pickingRest1.setWodStatus(5);
                            pickingRestMapper.updateById(pickingRest1);
                            pickingRestDbDto.setCode("200");
                            return pickingRestDbDto;
                        }
                        //判断是否有交接
                        int docHandoverMark = sysDocument.getDocHandoverMark();
                        //有交接
                        if (docHandoverMark == 1) {
                            //修改单据为待交接
                            pickingRest1.setWodStatus(6);
                            pickingRestMapper.updateById(pickingRest1);

                        } else {
                            //修改单据为yiwanc
                            pickingRest1.setWodStatus(3);
                            //删除库存
                            wmsStockInfoMapper.delete(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, inputValue).eq(WmsStockInfo::getWsiMaterialStatus, 5));
                            pickingRestMapper.updateById(pickingRest1);
                        }
                    }
                } else {
                    //计划出库重量
                    Double allotAmount = Double.parseDouble(pickingRest1.getPlannedWeight() + "");
                    //实际出库数量
                    Integer allotNumOut = pickingRest1.getOutgoingNumber();
                    if (allotNumOut >= allotAmount) {
                        //判断是否检验或者交接
                        //判断是否有检验
                        int isDocInspectionMark = sysDocument.getDocInspectionMark();
                        //如果有检验
                        if (isDocInspectionMark == 1) {
                            //生成品质检验单
                            QualityInspectionDocumentVo qualityInspectionDocumentVo = new QualityInspectionDocumentVo();
                            //添加类别，编码，物料类别
                            qualityInspectionDocumentVo.setMaterialCode(pickingRest.getMaterialCode());
                            qualityInspectionDocumentVo.setMaterialCategory(publicGoods.getMaterialType());
                            qualityInspectionDocumentVo.setCategory(publicGoods.getMaterialCategory());
                            qualityInspectionDocumentVo.setAssociatedNo(pickingRest.getWodDocNum());
                            qualityInspectionDocumentVo.setModeType(1);
                            List<MaterialNumberAndBatchNoVo> materialNumberAndBatchNoVos = new ArrayList<>();
                            //查询当前明细表中的所有物料号码
                            List<PickingRestDetail> stockOutInfoDetailList = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));

                            String batchNum = "";

                            for (int j = 0; j < stockOutInfoDetailList.size(); j++) {
                                if (j == 0) {
                                    batchNum = stockOutInfoDetailList.get(j).getMaterialBatch();
                                } else {
                                    if (!batchNum.equals(stockOutInfoDetailList.get(j).getMaterialBatch())) {
                                        batchNum = batchNum + "," + stockOutInfoDetailList.get(j).getMaterialBatch();
                                    }
                                }

                                MaterialNumberAndBatchNoVo materialNumberAndBatchNoVo = new MaterialNumberAndBatchNoVo();
                                materialNumberAndBatchNoVo.setMaterialNumber(stockOutInfoDetailList.get(j).getMaterialNumber());
                                materialNumberAndBatchNoVo.setBatchNo(stockOutInfoDetailList.get(j).getMaterialBatch());
                                materialNumberAndBatchNoVo.setMaterialCategory(publicGoods.getMaterialCategory());
                                materialNumberAndBatchNoVos.add(materialNumberAndBatchNoVo);
                            }
                            qualityInspectionDocumentVo.setBatchNo(batchNum);

                            productionPickingService.insertQuality(sysDocument, qualityInspectionDocumentVo, materialNumberAndBatchNoVos);
                            //修改单据为待检验
                            pickingRest1.setWodStatus(5);
                            pickingRestMapper.updateById(pickingRest1);
                            pickingRestDbDto.setCode("200");
                            return pickingRestDbDto;
                        }
                        //判断是否有交接
                        int docHandoverMark = sysDocument.getDocHandoverMark();
                        //有交接
                        if (docHandoverMark == 1) {
                            //修改单据明细为待交接
                            pickingRest1.setWodStatus(6);
                            pickingRestMapper.updateById(pickingRest1);

                        } else {
                            //修改单据状态为已完成
                            pickingRest1.setWodStatus(3);
                            //删除库存
                            wmsStockInfoMapper.delete(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, inputValue).eq(WmsStockInfo::getWsiMaterialStatus, 5));
                            pickingRestMapper.updateById(pickingRest1);
                        }
                    }
                }
            }


            // 条码撤销
        } else if ("3".equals(type)) {
            PickingRest pickingRest = pickingRestMapper.selectById(pickingRestDbVo.getId());


            if (pickingRest.getWodStatus()!=1&&pickingRest.getWodStatus()!=2){
                throw new GlobalException("只能对待出库和出库中的单据进行条码撤销");
            }

            // 查询明细表是否已经扫描
            PickingRestDetail pickingRestDetail = pickingRestDetailMapper.selectOne(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getMaterialNumber, pickingRestDbVo.getInputValue()).eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));
            if (pickingRestDetail == null) {
                pickingRestDbDto.setCode("500");
                pickingRestDbDto.setMessage("撤销失败,请核实后再录入");
                return pickingRestDbDto;
            }// 修改库存状态为在库
            OutboundLog outboundLog = outboundLogMapper.selectOne(new QueryWrapper<OutboundLog>().lambda().eq(OutboundLog::getWsiMaterialNumber, inputValue));
            //删除日志
            outboundLogService.removeById(outboundLog);


            WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, pickingRestDbVo.getInputValue()));
            wmsStockInfo.setWsiMaterialStatus(2);
            wmsStockInfoMapper.updateById(wmsStockInfo);

            // 删除其他出库明细信息
            pickingRestDetailMapper.delete(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getMaterialNumber, pickingRestDbVo.getInputValue()).eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));


            BigDecimal out = new BigDecimal(String.valueOf(pickingRest.getOutgoingWeight()));
            BigDecimal bigDecimal = new BigDecimal(String.valueOf(wmsStockInfo.getWsiNetWeight()));
            BigDecimal outWeight = out.subtract(bigDecimal);
            //修改出库重量
            pickingRest.setOutgoingWeight(outWeight.doubleValue());

            //扣除数量
            int num = pickingRest.getOutgoingNumber();
            pickingRest.setOutgoingNumber(num - 1);

            //查询单位
            PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, pickingRest.getMaterialCode()));

            //实际出库重量
            if ("2".equals(publicGoods.getMaterialUnit())) {

                BigDecimal sj = new BigDecimal(String.valueOf(pickingRest.getActualNumber()));
                BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(wmsStockInfo.getWsiNetWeight()));
                BigDecimal sjWeight = sj.subtract(bigDecimal1);
                pickingRest.setActualNumber(sjWeight.doubleValue());
            }else if("3".equals(publicGoods.getMaterialUnit())){
                BigDecimal sj = new BigDecimal(String.valueOf(pickingRest.getActualNumber()));
                BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(wmsStockInfo.getWsiNetWeight())).divide(new BigDecimal("1000"));
                BigDecimal sjWeight = sj.subtract(bigDecimal1);
                pickingRest.setActualNumber(sjWeight.doubleValue());

            } else {
                BigDecimal sj = new BigDecimal(String.valueOf(pickingRest.getActualNumber()));
                BigDecimal bigDecimal1 = new BigDecimal("1");
                BigDecimal sjWeight = sj.subtract(bigDecimal1);
                pickingRest.setActualNumber(sjWeight.doubleValue());
            }

            int i = pickingRestMapper.updateById(pickingRest);
            if (i <= 0) {
                pickingRestDbDto.setCode("500");
                pickingRestDbDto.setMessage("撤销失败");
                return pickingRestDbDto;
            }

        }

        pickingRestDbDto.setCode("200");
        return pickingRestDbDto;
    }


    /**
     * 其他出库拣货完成
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean pickingRestCompleted(Long id) throws Exception {

        // 获取菜单id
        Long idByMenuName = feignService.getIdByMenuName("其他出库");
        AjaxResult infoDocumentConfigById = systemDocumentFeignService.getInfoDocumentConfigById(idByMenuName);
        SysDocument sysDocument = com.alibaba.fastjson2.JSON.parseObject(JSON.toJSONString(infoDocumentConfigById.get("data")), SysDocument.class);

        PickingRest pickingRest = pickingRestMapper.selectById(id);
        //判断是否有检验
        int docInspectionMark = sysDocument.getDocInspectionMark();
        //如果有检验
        if (docInspectionMark == 1) {

            PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, pickingRest.getMaterialCode()));
            //生成品质检验单
            QualityInspectionDocumentVo qualityInspectionDocumentVo = new QualityInspectionDocumentVo();
            //添加类别，编码，物料类别
            qualityInspectionDocumentVo.setMaterialCode(pickingRest.getMaterialCode());
            qualityInspectionDocumentVo.setMaterialCategory(publicGoods.getMaterialType());
            qualityInspectionDocumentVo.setCategory(publicGoods.getMaterialCategory());
            qualityInspectionDocumentVo.setAssociatedNo(pickingRest.getWodDocNum());
            qualityInspectionDocumentVo.setModeType(1);
            List<MaterialNumberAndBatchNoVo> materialNumberAndBatchNoVos = new ArrayList<>();
            //查询当前明细表中的所有物料号码
            List<PickingRestDetail> stockOutInfoDetailList = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));


            String batchNum = "";

            for (int j = 0; j < stockOutInfoDetailList.size(); j++) {
                if (j == 0) {
                    batchNum = stockOutInfoDetailList.get(j).getMaterialBatch();
                } else {
                    if (!batchNum.equals(stockOutInfoDetailList.get(j).getMaterialBatch())) {
                        batchNum = batchNum + "," + stockOutInfoDetailList.get(j).getMaterialBatch();
                    }
                }

                MaterialNumberAndBatchNoVo materialNumberAndBatchNoVo = new MaterialNumberAndBatchNoVo();
                materialNumberAndBatchNoVo.setMaterialNumber(stockOutInfoDetailList.get(j).getMaterialNumber());
                materialNumberAndBatchNoVo.setBatchNo(stockOutInfoDetailList.get(j).getMaterialBatch());
                materialNumberAndBatchNoVo.setMaterialCategory(publicGoods.getMaterialCategory());
                materialNumberAndBatchNoVos.add(materialNumberAndBatchNoVo);
            }
            qualityInspectionDocumentVo.setBatchNo(batchNum);

            productionPickingService.insertQuality(sysDocument, qualityInspectionDocumentVo, materialNumberAndBatchNoVos);
            //修改单据状态为待检验
            pickingRest.setWodStatus(5);
            return pickingRestMapper.updateById(pickingRest) > 0;
        }

        //判断是否有交接
        int docHandoverMark = sysDocument.getDocHandoverMark();
        //有交接
        if (docHandoverMark == 1) {
            //修改单据状态为待交接
            pickingRest.setWodStatus(6);
            return pickingRestMapper.updateById(pickingRest) > 0;
        }

        //判断是否有超收
        int docHairMark = sysDocument.getDocHairMark();

        //修改单据状态为已完成
        pickingRest.setWodStatus(3);

        //删除库存
        List list = new ArrayList<WmsStockInfo>();

        List<PickingRestDetail> pickingRestDetailList3 = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));

        for (PickingRestDetail pickingRestDetail : pickingRestDetailList3) {
            WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, pickingRestDetail.getMaterialNumber()));
            list.add(wmsStockInfo);
        }
        wmsStockInfoMapper.deleteBatchIds(list);

        return pickingRestMapper.updateById(pickingRest) > 0;

    }

    /**
     * 其他出库拣货撤销
     *
     * @param id
     * @return
     */
    @Override
    public boolean pickingRestRepeal(Long id) {
        // 修改单据状态为待入库
        PickingRest pickingRest = pickingRestMapper.selectById(id);
        Integer wodStatus = pickingRest.getWodStatus();

        if (wodStatus != 2 && wodStatus != 5 && wodStatus != 6) {
            throw new GlobalException("状态为“出库中、待检验、待交接”的单据才可进行撤消;");
        }

        pickingRest.setWodStatus(1);
        // 重量扣除、数量清零
        pickingRest.setActualNumber(0.0);
        pickingRest.setOutgoingWeight(0.0);
        pickingRest.setOutgoingNumber(0);

        //交接数量，交接重量清空
        pickingRest.setOverWeight(0.0);
        pickingRest.setOverNum(0);
        pickingRestMapper.updateById(pickingRest);

        List<PickingRestDetail> pickingRestDetailList = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));

        //日志表集合
        List<OutboundLog> outboundLogList = new ArrayList<>();

        // 修改仓库状态为在库
        for (PickingRestDetail pickingRestDetail : pickingRestDetailList) {
            WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, pickingRestDetail.getMaterialNumber()));
            wmsStockInfo.setWsiMaterialStatus(2);
            wmsStockInfoMapper.updateById(wmsStockInfo);

            OutboundLog outboundLog = outboundLogMapper.selectOne(new QueryWrapper<OutboundLog>().lambda().eq(OutboundLog::getWsiMaterialNumber, wmsStockInfo.getWsiMaterialNumber()));
            outboundLogList.add(outboundLog);

        }
        //删除日志表中的数据
        outboundLogService.removeBatchByIds(outboundLogList);

        //删除品质检验单
        qualityFeginService.removeDoc(pickingRest.getWodDocNum());
        // 删除所有明细
        int delete = pickingRestDetailMapper.delete(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));

        return delete > 0;
    }

    /**
     * 其他出库交接
     *
     * @param pickingRestDbVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public PickingRestHandoverDto pickingRestHandover(PickingRestDbVo pickingRestDbVo) {

        // 获取步骤
        int step = pickingRestDbVo.getExecutionOrder();
        String inputValue = pickingRestDbVo.getInputValue();

        // 返回值
        PickingRestHandoverDto pickingRestHandoverDto = new PickingRestHandoverDto();

        //获取菜单id
        Long menuId = feignService.getIdByMenuName("其他出库");
        //交接方式
        AjaxResult infoDocumentConfigById = systemDocumentFeignService.getInfoDocumentConfigById(menuId);
        SysDocument sysDocument = JSON.parseObject(JSON.toJSONString(infoDocumentConfigById.get("data")), SysDocument.class);
        //拿到交接方式
        int jjType = sysDocument.getDocHandoverMethod();

        //根据字典查出单位
        List<SysDictData> unitList = DictUtils.getDictCache("sys_material_unit");
        Map<String, String> unitMap = unitList.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));

        // 当顺序为步骤一
        if (step == 1) {


                //校验员工编号
                AjaxResult byUserName = remoteUserService.getByUserName(pickingRestDbVo.getInputValue());
                SysUser userInfo = JSON.parseObject(JSON.toJSONString(byUserName.get("data")), SysUser.class);

                if (userInfo.getUserName() == null) {
                    pickingRestHandoverDto.setResultCode("500");
                    pickingRestHandoverDto.setMessage("交接人输入有误,请核对后输入");
                    return pickingRestHandoverDto;
                }

                //返回交接人
                pickingRestHandoverDto.setHandoverName(userInfo.getUserName() + "-" + userInfo.getNickName());
                pickingRestHandoverDto.setDocHandoverMethod(jjType);



            if (jjType == 1) {
                pickingRestHandoverDto.setMessage("请输入物料条码");
            } else if (jjType == 2) {
                pickingRestHandoverDto.setMessage("请输入批次号");
            } else if (jjType == 3) {
                PickingRest pickingRest = pickingRestMapper.selectById(pickingRestDbVo.getId());

                pickingRestDbVo.setInputValue(pickingRest.getWodDocNum());

                //查询物料单位
                PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, pickingRest.getMaterialCode()));

                //判断是否存在明细表中
                List<PickingRestDetail> pickingRestDetails = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getWodDocNum, pickingRestDbVo.getInputValue()));
                if (pickingRestDetails == null) {
                    pickingRestHandoverDto.setResultCode("500");
                    pickingRestHandoverDto.setMessage("交接失败");
                    return pickingRestHandoverDto;
                }

                //查询当前单据号在明细中是否存在，且状态为待交接
                List<PickingRestDetail> pickingRestDetails1 = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getWodDocNum, pickingRestDbVo.getInputValue()).eq(PickingRestDetail::getMaterialStatus, 2));
                if (pickingRestDetails1.size() == 0) {
                    pickingRestHandoverDto.setMessage("交接失败,请核实后再录入");
                    pickingRestHandoverDto.setResultCode("500");
                    return pickingRestHandoverDto;
                } else {
                    //查出当前为待交接状态的单据明细,算出总净重和总数量
                    Double sumPiHao = pickingRestDetailMapper.getDoc(pickingRestDbVo.getInputValue());
                    //总数量
                    long sumNum = pickingRestDetails1.size();

                    //出库重量
                    Double allotAmount = pickingRest.getOutgoingWeight();

                    //返回计划/实际

                    //(实际交接)
                    Double overWeight = pickingRest.getOverWeight();
                    if (overWeight == null) {
                        overWeight = 0.0;
                    }
                    BigDecimal bigDecimal = new BigDecimal(String.valueOf(overWeight));
                    //查询号码在明细中的重量
                    BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(sumPiHao));
                    BigDecimal add = bigDecimal.add(bigDecimal1);

                    pickingRest.setOverWeight(add.doubleValue());

                    pickingRestHandoverDto.setQuantityIdentification(allotAmount + "/" + add.toString() + unitMap.get(publicGoods.getMaterialUnit()));


                    //保存交接数量，交接重量
                    Integer overNum = pickingRest.getOverNum();
                    if (overNum == null) {
                        overNum = 0;
                    }
                    int jjSum = (int) sumNum;
                    pickingRest.setOverNum(overNum + jjSum);

                    //取出关节人
                    String closeUser = pickingRest.getCloseMan();
                    if (closeUser == null) {
                        closeUser = "";
                    }
                    String[] split = closeUser.split(",");

                    //获取交接人名称
                    String name = SecurityUtils.getUsername();

                    //定义变量记录交接人是否相等
                    int jj = 0;

                    if (closeUser != null && !"".equals(closeUser)) {
                        for (String s : split) {
                            if (!name.equals(s)) {
                                jj = 1;
                            }
                        }
                    } else {
                        pickingRest.setCloseMan(name);
                    }


                    if (jj == 1) {
                        pickingRest.setCloseMan("," + name);
                    }
                    pickingRest.setCloseTime(new Date());

                    //出库/交接
                    pickingRestHandoverDto.setOutHandoverActual(pickingRest.getOutgoingNumber() + "/" + pickingRest.getOverNum());

                    for (PickingRestDetail pickingRestDetail : pickingRestDetails1) {
                        //修改物料状态明细为已出库
                        pickingRestDetail.setMaterialStatus(3);
                        pickingRestDetailMapper.updateById(pickingRestDetail);
                    }


                    //判断是否所有物料没有待交接的状态
                    List<PickingRestDetail> pickingRestDetails2 = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getMaterialStatus, 2).eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));

                    if (pickingRestDetails2.size() == 0) {
                        //修改单据状态为已完成
                        pickingRest.setWodStatus(3);

                        //删除库存
                        List list = new ArrayList<WmsStockInfo>();

                        List<PickingRestDetail> pickingRestDetailList3 = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));

                        for (PickingRestDetail pickingRestDetail : pickingRestDetailList3) {
                            WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, pickingRestDetail.getMaterialNumber()));
                            list.add(wmsStockInfo);
                        }
                        wmsStockInfoMapper.deleteBatchIds(list);

                    }
                    boolean b = updateById(pickingRest);
                    if (!b) {
                        pickingRestHandoverDto.setResultCode("500");
                        pickingRestHandoverDto.setMessage("交接失败");
                        return pickingRestHandoverDto;
                    }

                    //消息提示
                    pickingRestHandoverDto.setMessage("单据号:" + pickingRestDbVo.getInputValue() + ",总净重" + sumPiHao + "数量:" + sumNum + ",交接成功");
                }
            }
            pickingRestHandoverDto.setResultCode("200");
            return pickingRestHandoverDto;
        }
        // 当顺序为步骤二
        if (step == 2) {
            // 单个
            if (jjType == 1) {
                PickingRest pickingRest = pickingRestMapper.selectById(pickingRestDbVo.getId());

                //查询物料单位
                PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, pickingRest.getMaterialCode()));

                // 判断是否存在明细表中
                List<PickingRestDetail> pickingRestDetails = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getMaterialNumber, pickingRestDbVo.getInputValue()).eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));
                if (pickingRestDetails == null) {
                    pickingRestHandoverDto.setResultCode("500");
                    pickingRestHandoverDto.setMessage("交接失败，请核实后录入");
                    return pickingRestHandoverDto;
                }

                // 判断当前物料是否存在且属于待交接
                PickingRestDetail pickingRestDetail = pickingRestDetailMapper.selectOne(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getMaterialNumber, pickingRestDbVo.getInputValue()).eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));
                if (pickingRestDetail.getMaterialStatus() != 2) {
                    pickingRestHandoverDto.setResultCode("500");
                    pickingRestHandoverDto.setMessage("不可重复交接");
                    return pickingRestHandoverDto;
                } else {
                    //出库重量
                    Double allotAmount = pickingRest.getOutgoingWeight();

                    Double overWeight = pickingRest.getOverWeight();
                    if (overWeight == null) {
                        overWeight = 0.0;
                    }
                    //(实际交接)
                    BigDecimal bigDecimal = new BigDecimal(String.valueOf(overWeight));
                    //查询号码在明细中的重量
                    BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(pickingRestDetail.getNetWeight()));
                    BigDecimal add = bigDecimal.add(bigDecimal1);

                    pickingRest.setOverWeight(add.doubleValue());

                    pickingRestHandoverDto.setQuantityIdentification(allotAmount + "/" + add.toString() + unitMap.get(publicGoods.getMaterialUnit()));

                    //保存交接数量，交接重量
                    Integer overNum = pickingRest.getOverNum();
                    if (overNum == null) {
                        overNum = 0;
                    }
                    pickingRest.setOverNum(overNum + 1);

                    //取出关节人
                    String closeUser = pickingRest.getCloseMan();
                    if (closeUser == null) {
                        closeUser = "";
                    }
                    String[] split = closeUser.split(",");

                    //获取交接人名称
                    String name = SecurityUtils.getUsername();

                    //定义变量记录交接人是否相等
                    int jj = 0;

                    if (closeUser != null && !"".equals(closeUser)) {
                        for (String s : split) {
                            if (!name.equals(s)) {
                                jj = 1;
                            }
                        }
                    } else {
                        pickingRest.setCloseMan(name);
                    }


                    if (jj == 1) {
                        pickingRest.setCloseMan("," + name);
                    }
                    pickingRest.setCloseTime(new Date());


                    //出库/交接
                    pickingRestHandoverDto.setOutHandoverActual(pickingRest.getOutgoingNumber() + "/" + pickingRest.getOverNum());

                    // 修改其他出库明细物料状态为已出库
                    pickingRestDetail.setMaterialStatus(3);
                    pickingRestDetailMapper.updateById(pickingRestDetail);

                    // 判断是否所有物料没有待交接的状态
                    List<PickingRestDetail> pickingRestDetails1 = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getMaterialStatus, 2).eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));
                    if (pickingRestDetails1.size() == 0) {
                        // 修改单据状态为已完成
                        pickingRest.setWodStatus(3);

                        //删除库存
                        List list = new ArrayList<WmsStockInfo>();

                        List<PickingRestDetail> pickingRestDetailList3 = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));

                        for (PickingRestDetail pickingRestDetail11 : pickingRestDetailList3) {
                            WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, pickingRestDetail11.getMaterialNumber()));
                            list.add(wmsStockInfo);
                        }
                        wmsStockInfoMapper.deleteBatchIds(list);
                    }

                    boolean b = updateById(pickingRest);
                    if (!b) {
                        pickingRestHandoverDto.setResultCode("500");
                        pickingRestHandoverDto.setMessage("交接失败");
                        return pickingRestHandoverDto;
                    }

                    //消息提示
                    pickingRestHandoverDto.setMessage("物料号码:" + pickingRestDbVo.getInputValue() + ",净重" + pickingRestDetail.getNetWeight() + "数量:1,交接成功");
                }

                // 批次
            } else if (jjType == 2) {

                PickingRest pickingRest = pickingRestMapper.selectById(pickingRestDbVo.getId());

                //查询物料单位
                PublicGoods publicGoods = publicGoodsMapper.selectOne(new QueryWrapper<PublicGoods>().lambda().eq(PublicGoods::getMaterialCode, pickingRest.getMaterialCode()));

                // 判断是否存在明细表中
                List<PickingRestDetail> pickingRestDetails = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getBreadth, pickingRestDbVo.getInputValue()).eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));
                if (pickingRestDetails == null) {
                    pickingRestHandoverDto.setResultCode("500");
                    pickingRestHandoverDto.setMessage("交接失败,请核实后再输入");
                    return pickingRestHandoverDto;
                }

                // 判断当前物料是否存在且属于待交接
                List<PickingRestDetail> pickingRestDetails2 = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getMaterialBatch, pickingRestDbVo.getInputValue()).eq(PickingRestDetail::getMaterialStatus, 2).eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));
                if (pickingRestDetails2 == null) {
                    pickingRestHandoverDto.setResultCode("500");
                    pickingRestHandoverDto.setMessage("不可重复交接");
                    return pickingRestHandoverDto;
                } else {
                    //查出当前为待交接状态的批次，算出总净重和总数量
                    Double sumPiHao = pickingRestDetailMapper.getPiHaoRest(pickingRestDbVo.getInputValue());
                    //总数量
                    long sumNum = pickingRestDetails2.size();

                    //出库重量
                    Double allotAmount = pickingRest.getOutgoingWeight();

                    //返回计划/实际

                    //(实际交接)
                    Double overWeight = pickingRest.getOverWeight();
                    if (overWeight == null) {
                        overWeight = 0.0;
                    }
                    BigDecimal bigDecimal = new BigDecimal(String.valueOf(overWeight));
                    //查询号码在明细中的重量
                    BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(sumPiHao));
                    BigDecimal add = bigDecimal.add(bigDecimal1);

                    pickingRest.setOverWeight(add.doubleValue());

                    pickingRestHandoverDto.setQuantityIdentification(allotAmount + "/" + add.toString() + unitMap.get(publicGoods.getMaterialUnit()));


                    //保存交接数量，交接重量
                    Integer overNum = pickingRest.getOverNum();
                    if (overNum == null) {
                        overNum = 0;
                    }
                    int jjSum = (int) sumNum;
                    pickingRest.setOverNum(overNum + jjSum);

                    //取出关节人
                    String closeUser = pickingRest.getCloseMan();
                    if (closeUser == null) {
                        closeUser = "";
                    }
                    String[] split = closeUser.split(",");

                    //获取交接人名称
                    String name = SecurityUtils.getUsername();

                    //定义变量记录交接人是否相等
                    int jj = 0;

                    if (closeUser != null && !"".equals(closeUser)) {
                        for (String s : split) {
                            if (!name.equals(s)) {
                                jj = 1;
                            }
                        }
                    } else {
                        pickingRest.setCloseMan(name);
                    }


                    if (jj == 1) {
                        pickingRest.setCloseMan("," + name);
                    }
                    pickingRest.setCloseTime(new Date());


                    //出库/交接
                    pickingRestHandoverDto.setOutHandoverActual(pickingRest.getOutgoingNumber() + "/" + pickingRest.getOverNum());

                    for (PickingRestDetail pickingRestDetail : pickingRestDetails2) {
                        //修改物料状态明细为已出库
                        pickingRestDetail.setMaterialStatus(3);

                    }

                    pickingRestDetailService.updateBatchById(pickingRestDetails2);

                    //判断是否所有物料没有待交接的状态
                    List<PickingRestDetail> pickingRestDetailList3 = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getMaterialStatus, 2).eq(PickingRestDetail::getWodDocNum, pickingRest.getWodDocNum()));

                    if (pickingRestDetailList3.size() == 0) {
                        //修改单据状态为已完成
                        pickingRest.setWodStatus(3);
                        //删除库存
                        wmsStockInfoMapper.delete(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, inputValue).eq(WmsStockInfo::getWsiMaterialStatus, 5));

                    }
                    boolean b = updateById(pickingRest);
                    if (!b) {
                        pickingRestHandoverDto.setMessage("交接失败");
                        pickingRestHandoverDto.setResultCode("500");
                        return pickingRestHandoverDto;
                    }
                    //消息提示
                    pickingRestHandoverDto.setMessage("批次号:" + pickingRestDbVo.getInputValue() + ",总净重" + sumPiHao + "数量:" + sumNum + ",交接成功");
                }

                // 整单
            } else if (jjType == 3) {
                pickingRestHandoverDto.setResultCode("500");
                pickingRestHandoverDto.setMessage("请勿重复交接");
            }
        }
        pickingRestHandoverDto.setResultCode("200");
        return pickingRestHandoverDto;
    }

    /**
     * 修改品质状态
     *
     * @param updateDocVo
     * @return
     */
    @Override
    public boolean restUpdateStatus(UpdateDocVo updateDocVo) {
        //获取菜单id
        Long menuId = feignService.getIdByMenuName("其他出库");
        //交接方式
        AjaxResult infoDocumentConfigById = systemDocumentFeignService.getInfoDocumentConfigById(menuId);
        SysDocument sysDocument = JSON.parseObject(JSON.toJSONString(infoDocumentConfigById.get("data")), SysDocument.class);
        //交接标志
        int docHandoverMethod = sysDocument.getDocHandoverMark();

        //判断是否有交接
        //如果有交接，修改单据状态和物料明细状态为代交接
        if (docHandoverMethod == 1) {
            PickingRest pickingRest = pickingRestMapper.selectOne(new QueryWrapper<PickingRest>().lambda().eq(PickingRest::getWodDocNum, updateDocVo.getDocNum()));
            pickingRest.setWodStatus(6);//待交接
            updateById(pickingRest);
            List<PickingRestDetail> pickingRestDetailList = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getWodDocNum, updateDocVo.getDocNum()));
            for (PickingRestDetail pickingRestDetail : pickingRestDetailList) {
                pickingRestDetail.setMaterialStatus(2);
            }
            return pickingRestDetailService.updateBatchById(pickingRestDetailList);

        } else {
            PickingRest pickingRest = pickingRestMapper.selectOne(new QueryWrapper<PickingRest>().lambda().eq(PickingRest::getWodDocNum, updateDocVo.getDocNum()));
            pickingRest.setWodStatus(3);//已完成
            updateById(pickingRest);
            List<PickingRestDetail> pickingRestDetailList = pickingRestDetailMapper.selectList(new QueryWrapper<PickingRestDetail>().lambda().eq(PickingRestDetail::getWodDocNum, updateDocVo.getDocNum()));

            List<WmsStockInfo> wmsStockInfoList = new ArrayList<>();

            for (PickingRestDetail pickingRestDetail : pickingRestDetailList) {
                pickingRestDetail.setMaterialStatus(3);

                WmsStockInfo wmsStockInfo = wmsStockInfoMapper.selectOne(new QueryWrapper<WmsStockInfo>().lambda().eq(WmsStockInfo::getWsiMaterialNumber, pickingRestDetail.getMaterialNumber()));
                wmsStockInfoList.add(wmsStockInfo);

            }
            wmsStockInfoService.removeBatchByIds(wmsStockInfoList);
            return pickingRestDetailService.updateBatchById(pickingRestDetailList);
        }
    }

    @Override
    public SchemeDto point(Long id) {
        PickingRest byId = getById(id);
        SchemeVo schemeVo = new SchemeVo();
        schemeVo.setMaterialCode(byId.getMaterialCode());
        schemeVo.setAssociatedDetails("4");
        SchemeDto schemeData = pickingSchemeService.getSchemeData(schemeVo);
        return schemeData;
    }


    /**
     * 新增其他出库物料明细
     *
     * @param pickingRestDetail
     * @return
     */
    @Override
    public boolean addRestDetail(PickingRestDetail pickingRestDetail) {
        return pickingRestDetailService.save(pickingRestDetail);
    }
}
